package com.softwareweb.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.softwareweb.dao.ConnDB;

/**
 * Servlet implementation class UserFormValidator
 */
public class UserFormValidator extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserFormValidator() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String r;
		ConnDB conn=new ConnDB();
		String checkCmd=new String();
		if((r=request.getParameter("username"))!=null)
		{
			checkCmd="SELECT * from user WHERE name = \""+r+"\"";
		}
		else if((r=request.getParameter("email"))!=null){
			checkCmd="SELECT * from user WHERE email = '"+r+"'";
		}
		else {
			PrintWriter writer=response.getWriter();
			writer.write("false");
			if(conn!=null)
				conn.close();
			return;
		}
		ResultSet rs=conn.executeQuery(checkCmd);
		try {
			boolean result=rs.next();
			if(result==false){
				PrintWriter writer=response.getWriter();
				writer.print("true");
				conn.close();
				return;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			if(conn!=null)
			{
				conn.close();
			}
			e.printStackTrace();
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
